/*
 * GToe: Gtk+ TicTacToe
 * player.c: player
 *
 * Copyright (C) 2010 Dani El-Soufi <danisoufi@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "../include/core.h"
#include "../include/player.h"

FORM   *form = NULL;
FIELD  **field;

PLAYER
new_player(void)
{
    int i;
    PLAYER p;
    int *mem = p.won;

    for (i = 0; i < 3; ++i)
    {
        mem++ = 0;
    }

    return p;
}

char *
get_pname(int player)
{
    int c;
    PLAYER p;
    char buffer[MAX_PNAME_LEN], *pek;
    int n = 0;

    curs_set(2);

    field = (FIELD **) calloc(1, sizeof(FIELD *));
    field[0] = new_field(1, 10, LINES / 4, (COLS / 4) + 15, 0, 0);
    field[1] = 0;

    item = (ITEM **) calloc(2, sizeof(ITEM *));
    item[0] = new_item("Back to main menu" ,"");
    item[2] = 0;

    field_opts_off(field[0], O_WRAP); /* Don't wrap text on multi-lines */
    field_opts_off(field[0], O_BLANK); /* Avoid field erasing */
    field_opts_off(field[0], O_AUTOSKIP); /* Avoid entering into same field when it's full */

    form = new_form(field);
    menu = new_menu(item);
    window = newwin(3, 19, (LINES / 2), (COLS / 2) - 8);
    set_menu_win(menu, window);
    set_menu_mark(menu, "");
    post_menu(menu);
    post_form(form);

    if (player == 0)
    {
        mvaddstr(LINES / 2, COLS / 4, "Player's name: ");
    }
    else if (player == 1)
    {
        mvaddstr(LINES / 2, COLS / 4, "Player 1 name: ");
    }
    else if (player == 2)
    {
        mvaddstr(LINES / 2, COLS / 4, "Player 2 name: ");
    }

    mvaddstr(LINES * 99/100, 0, "Press \"Enter\" key to confirm player's name.");

    refresh();
    wrefresh(window);
 
    while(c = getch())
    {
        switch(c)
        {
            case KEY_DOWN:
                curs_set(0);

                menu_driver(menu, REQ_DOWN_ITEM);

                int select = get_menu_item();

                if (select == 0)
                {
                    gtoe_select_menu();
                }
                break;
            case KEY_LEFT:
                form_driver(form, REQ_LEFT_CHAR);
                break;
            case KEY_RIGHT:
                form_driver(form, REQ_RIGHT_CHAR);
                break;
            case KEY_BACKSPACE:
                form_driver(form, REQ_PREV_CHAR);
                form_driver(form, REQ_DEL_CHAR);
                break;
            case 0xA:
                snprintf(buffer, MAX_PNAME_LEN, "%s", field_buffer(field[0], 0));
                pek = buffer;
                break;
            default: /* char input */
                form_driver(form, c);
        }
        wrefresh(window);
    }

    /* Skip initial white space. */
    while (*pek == ' ' || *pek == '\t' || *pek == '\r')
    {
        ++pek;
    }

    while (*pek && (n < maxlen -1) && (n < sizeof(buffer) - 1))
    {
        if (*pek == '\n')
        {
            break;
        }

        /* Convert CR and TAB to a simple space. */
        if (*pek == '\r' || *pek == '\t')
        {
           *pek = ' ';
        }

        /* Avoid duplicate white spaces. */
        if ((n > 0) && (p.name[n - 1] == ' ') && (*pek == ' '))
        {
           ++pek;
           continue;
        }

        p.name[n++] = *(pek++);
     }

    /* Skip trailing white spaces too. */
    while ((n > 0) && (p.name[n - 1] == ' '))
    {
        --n;
    }

    /* Set something for an empty name. */
    if (n == 0)
    {
        strcpy(p.name, "Player 1");
    }
    else
    {
        p.name[n] = '\0';
    }

    return p.name;
}

void
get_player1_move(void)
{
    char pmove[4];
    int success;

    printw("%s, playing X, it is your turn: ", p1.name);

    /* TODO: read cell coordinates and set stone */
    success = set_matrix(matrix, x, y, X);

}

void
get_player2_move(void)
{
    char pmove[4];
    int success;

    printw("%s, playing O, it is your turn: ", p2.name);

    /* TODO: read cell coordinates and set stone */
    success = set_matrix(matrix, x, y, X);
}
